/**
 * 数据处理
 * @author echo.
 * @version 1.5.0
 **/
const utils = {
	/**
	 * 是否是微信浏览器
	 * */
	isWeiXin: function() {
		//window.navigator.userAgent属性包含了浏览器类型、版本、操作系统类型、浏览器引擎类型等信息，这个属性可以用来判断浏览器类型
		var ua = window.navigator.userAgent.toLowerCase();
		alert(JSON.stringify(ua))
		//通过正则表达式匹配ua中是否含有MicroMessenger字符串
		return ua.match(/MicroMessenger/i) == 'micromessenger';
	},
	//去空格
	trim: function(value) {
		return value.replace(/(^\s*)|(\s*$)/g, "");
	},
	//内容替换
	replaceAll: function(text, repstr, newstr) {
		return text.replace(new RegExp(repstr, "gm"), newstr);
	},
	//格式化手机号码
	formatNumber: function(num) {
		return num.length === 11 ? num.replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2') : num;
	},
	//金额格式化
	rmoney: function(money) {
		return parseFloat(money).toFixed(2).toString().split('').reverse().join('').replace(/(\d{3})/g, '$1,').replace(
			/\,$/, '').split('').reverse().join('');
	},
	/**
	 * 
	 * */
	//日期格式化
	formatDate: function(fdate, format = 'yyyy-MM-dd hh:mm:ss') {
		if (fdate) {
			let date = fdate;
			if (typeof(fdate) === "number") {
				//说明是一个时间戳
				if (fdate.toString().length === 10) {
					//10位的时间戳
					date = new Date(fdate * 1000);
				} else {
					date = new Date(fdate);
				}
			} else if (typeof(fdate) === "string") {
				if (fdate.indexOf("T") >= 0) {
					var dateee = new Date(fdate.replace("T", " ").replace(/(\.\d+)$/, "")).toJSON();
					var dateValue = new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(
						/\.[\d]{3}Z/, '');
					date = new Date(dateValue.replace(/-/g, "/"));
				} else if (fdate.indexOf("/Date") >= 0) {
					date = new Date(parseInt(time.slice(6, 19)));
				} else {
					date = new Date(fdate.replace(/-/g, "/"));
				}
			}
			if (!(date instanceof Date))
				date = new Date(time.replace(/-/g, "/"));
			if (format.indexOf("yyyy") >= 0)
				format = format.replace("yyyy", date.getFullYear());
			if (format.indexOf("MM") >= 0)
				format = format.replace("MM", (date.getMonth() + 1) <= 9 ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1));
			if (format.indexOf("dd") >= 0)
				format = format.replace("dd", date.getDate() <= 9 ? "0" + date.getDate() : date.getDate());
			if (format.indexOf("HH") >= 0 || format.indexOf("hh") >= 0)
				format = format.replace(/hh/i, date.getHours() <= 9 ? "0" + date.getHours() : date.getHours());
			if (format.indexOf("mm") >= 0)
				format = format.replace("mm", date.getMinutes() <= 9 ? "0" + date.getMinutes() : date.getMinutes());
			if (format.indexOf("ss") >= 0)
				format = format.replace("ss", date.getSeconds() <= 9 ? "0" + date.getSeconds() : date.getSeconds());
			return format;
		}
		return fdate;
	},
	rgbToHex: function(r, g, b) {
		return "#" + utils.toHex(r) + utils.toHex(g) + utils.toHex(b)
	},
	toHex: function(n) {
		n = parseInt(n, 10);
		if (isNaN(n)) return "00";
		n = Math.max(0, Math.min(n, 255));
		return "0123456789ABCDEF".charAt((n - n % 16) / 16) +
			"0123456789ABCDEF".charAt(n % 16);
	},
	hexToRgb(hex) {
		let result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
		return result ? {
			r: parseInt(result[1], 16),
			g: parseInt(result[2], 16),
			b: parseInt(result[3], 16)
		} : null;
	},
	/**
	 * 深度克隆
	 * */
	deepClone(obj) {
		let that = this;
		let temp = null;
		if (obj instanceof Array) {
			temp = obj.concat();
		} else if (obj instanceof Function) {
			//函数是共享的是无所谓的，js也没有什么办法可以在定义后再修改函数内容
			temp = obj;
		} else {
			temp = new Object();
			for (let i in obj) {
				if (obj.hasOwnProperty(i)) {
					temp[i] = typeof obj[i] === "object" && obj[i]!=null ? that.deepClone(obj[i]) : obj[i];
				}
			}
		}
		return temp;
	}
}

module.exports = {
	trim: utils.trim,
	replaceAll: utils.replaceAll,
	formatNumber: utils.formatNumber,
	rmoney: utils.rmoney,
	formatDate: utils.formatDate,
	rgbToHex: utils.rgbToHex,
	hexToRgb: utils.hexToRgb,
	deepClone: utils.deepClone,
}
